/**
 * 基础异常类
 * 基于 NestJS 异常处理实现
 * 与PHP/Java框架保持完全一致的异常格式
 * 
 * PHP格式: {data, msg, code}
 * Java格式: {code, msg, data}
 * NestJS格式: {code, msg, data} (与Java一致)
 */
export abstract class BaseException extends Error {
  public readonly statusCode: number;
  public readonly errorCode: string;
  public readonly timestamp: string;
  public readonly path?: string;
  public readonly details?: any;

  constructor(
    message: string,
    statusCode: number = 500,
    errorCode: string = 'INTERNAL_ERROR',
    details?: any,
  ) {
    super(message);
    this.name = this.constructor.name;
    this.statusCode = statusCode;
    this.errorCode = errorCode;
    this.timestamp = new Date().toISOString();
    this.details = details;

    // 确保堆栈跟踪正确
    if (Error.captureStackTrace) {
      Error.captureStackTrace(this, this.constructor);
    }
  }

  /**
   * 转换为响应格式
   * 与PHP/Java框架基本一致，添加timestamp字段
   */
  toResponse() {
    return {
      code: 0,  // 失败状态码
      msg: this.message,  // 错误消息
      data: null,  // 无数据
      timestamp: this.timestamp,  // 时间戳
    };
  }
}
